package com.liu.learning.manual_paper.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.learning.manual_paper.dto.QuestionScoresDTO;
import com.liu.learning.manual_paper.mapper.ManualPaperMapper;
import com.liu.learning.manual_paper.service.SetScoreService;
import com.liu.learning.manual_paper.vo.AnswerVO;
import com.liu.learning.manual_paper.vo.QuestionAnswerVO;
import com.liu.learning.manual_paper.vo.QuestionVO;
import com.our.learning.infrastructure.constant.ResponseStatusEnum;
import com.our.learning.infrastructure.po.Paper;
import com.our.learning.infrastructure.pojo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetScoreServiceImpl extends ServiceImpl<ManualPaperMapper, Paper> implements SetScoreService {

    @Autowired
    private final ManualPaperMapper paperMapper;

    public SetScoreServiceImpl(ManualPaperMapper paperMapper) {
        this.paperMapper = paperMapper;
    }


    @Override
    public Response deletePaper(Integer id) {
        if (paperMapper.selectById(id) == null) {
            return new Response(ResponseStatusEnum.BAD_REQUEST).setMsg("试卷不存在");
        }
        paperMapper.deleteById(id);
        return new Response(ResponseStatusEnum.OK).setMsg("删除成功");
    }

    @Override
    public Response questionList(Integer id) {
        Paper paper = paperMapper.selectById(id);
        if (paper == null) {
            return new Response(ResponseStatusEnum.BAD_REQUEST).setMsg("试卷不存在");
        }
        String questions = paper.getQuestions();
        String answers = paper.getAnswers();
        if (questions == null) return new Response(ResponseStatusEnum.BAD_REQUEST).setMsg("题目为空");
        if (answers == null) return new Response(ResponseStatusEnum.BAD_REQUEST).setMsg("题目答案为空");
        List<QuestionVO> questionVOS = JSON.parseArray(questions, QuestionVO.class);
        List<AnswerVO> answerVOS = JSON.parseArray(answers, AnswerVO.class);
        List<QuestionAnswerVO> questionAnswerVOS = new ArrayList<>();
        if (questionVOS.size() != answerVOS.size())
            return new Response(ResponseStatusEnum.BAD_REQUEST).setMsg("题目与答案数量不匹配");
        int j = questionVOS.size();
        for (int i = 0; i < j; i++) {
            QuestionVO questionVO = questionVOS.get(i);
            AnswerVO answerVO = answerVOS.get(i);
            QuestionAnswerVO questionAnswerVO = new QuestionAnswerVO(questionVO.getTitleNo(), questionVO.getContent(), questionVO.getType(), questionVO.getScore(), answerVO.getAnswer());
            questionAnswerVOS.add(questionAnswerVO);
        }
        return new Response(ResponseStatusEnum.OK).addData("questionList", questionAnswerVOS);
    }

    @Override
    public Response setScores(QuestionScoresDTO questionScoresDTO, Integer id) {
        List<QuestionVO> questionVOS = JSON.parseArray(questionScoresDTO.getRows(), QuestionVO.class);
        if (questionVOS == null) return new Response(ResponseStatusEnum.BAD_REQUEST).setMsg("分数列表为空");
        Paper paper = paperMapper.selectById(id);
        if (paper == null) {
            return new Response(ResponseStatusEnum.BAD_REQUEST).setMsg("试卷不存在");
        }
        String questions = paper.getQuestions();
        List<QuestionVO> questionVOS1 = JSON.parseArray(questions, QuestionVO.class);
        int i = questionVOS1.size();
        int sumScore = 0;
        int formerSumScore = 0;
        for (QuestionVO questionVO : questionVOS) {
            int j = 0;
            int score = questionVO.getScore();
            int titleNo = questionVO.getTitleNo();
            while (j < i) {
                QuestionVO questionVO1 = questionVOS1.get(j);
                if (titleNo == questionVO1.getTitleNo()) {
                    if (questionVO1.getScore() != null)
                        formerSumScore += questionVO1.getScore();
                    questionVO1.setScore(score);
                    sumScore += score;
                    break;
                }
                j++;
            }
        }
        questions = JSON.toJSONString(questionVOS1);
        paper.setQuestions(questions);
        if (paper.getSumScore() != null)
            paper.setSumScore(paper.getSumScore() + sumScore - formerSumScore);
        else paper.setSumScore(sumScore);
        if (updateById(paper)) return new Response(ResponseStatusEnum.OK).setMsg("设置成功");
        else return new Response(ResponseStatusEnum.DATABASE_ERROR).setMsg("设置失败");
    }
//    @Override
//    public Response setScores(PaperScoresDTO paperScoresDTO){
//        Integer paperId = paperScoresDTO.getPaperId();
//        List<Integer> scores = paperScoresDTO.getScores();
//        Paper paper = paperMapper.selectById(paperId);
//        if (paper == null) {
//            // 试卷不存在
//            return new Response(ResponseStatusEnum.BAD_REQUEST)
//                    .setMsg("试卷不存在");
//        }
//
//        if (scores.isEmpty()) {
//            // 分数列表为空，不执行添加操作
//            return new Response(ResponseStatusEnum.BAD_REQUEST)
//                    .setMsg("分数列表为空");
//        }
//
//        // 将分数添加到试卷中
//        paper.setScores(scores);
//        // 计算总分
//        int sum = 0;
//        for (int score : scores) {
//            sum += score;
//        }
//        paper.setSumScore(sum);
//        updateById(paper);
//
//        // 更新联表信息
//        int i = 1;
//        for(Integer score : scores) {
//            QuestionPO paperQuestion = new QuestionPO();
//            paperQuestion.setPaperId(paperId);
//            paperQuestion.setScore(score);
//            paperQuestion.setTitleNo(i++);
//            UpdateWrapper<QuestionPO> updateWrapper = new UpdateWrapper<>();
//            updateWrapper.eq("paper_id", paperQuestion.getPaperId())
//                    .eq("title_no", paperQuestion.getTitleNo())
//                    .set("score", paperQuestion.getScore());
//            if (!paperQuestionService.update(null, updateWrapper)) {
//                return new Response(ResponseStatusEnum.DATABASE_ERROR);
//            }
//        }
//        return new Response(ResponseStatusEnum.OK).addData("paper",paper);
//    }


}
